import request from "./request"

// 获取用户列表
export const getAll = async () => {
    try {
        const data = await request.get('/api/user/get-all')
        return { 
            data,
            message: '获取数据成功',
            code: 200
        }
    } catch (error: any) {
        console.error('获取用户数据失败:', error)
        return { 
            data: [],
            message: error.message || '获取数据失败',
            code: 500
        }
    }
}

// 获取用户列表（分页）
export const getUsers = async (page: number = 1, pageSize: number = 10) => {
    try {
        const data = await request.get(`/api/users?page=${page}&pageSize=${pageSize}`)
        return { 
            data,
            message: '获取数据成功',
            code: 200
        }
    } catch (error: any) {
        console.error('获取用户数据失败:', error)
        return { 
            data: {
                list: [],
                pagination: {
                    page: 1,
                    pageSize: 10,
                    total: 0,
                    totalPage: 0
                }
            },
            message: error.message || '获取数据失败',
            code: 500
        }
    }
}

// 获取用户详情
export const getUserById = async (id: number) => {
    try {
        const data = await request.get(`/api/users/${id}`)
        return { 
            data,
            message: '获取数据成功',
            code: 200
        }
    } catch (error: any) {
        console.error('获取用户详情失败:', error)
        return { 
            data: null,
            message: error.message || '获取数据失败',
            code: 500
        }
    }
}

// 创建用户
export const createUser = async (userData: any) => {
    try {
        const data = await request.post('/api/users', userData)
        return { 
            data,
            message: '创建用户成功',
            code: 200
        }
    } catch (error: any) {
        console.error('创建用户失败:', error)
        return { 
            data: null,
            message: error.message || '创建用户失败',
            code: 500
        }
    }
}

// 更新用户
export const updateUser = async (id: number, userData: any) => {
    try {
        const data = await request.put(`/api/users/${id}`, userData)
        return { 
            data,
            message: '更新用户成功',
            code: 200
        }
    } catch (error: any) {
        console.error('更新用户失败:', error)
        return { 
            data: null,
            message: error.message || '更新用户失败',
            code: 500
        }
    }
}

// 删除用户
export const deleteUser = async (id: number) => {
    try {
        const data = await request.delete(`/api/users/${id}`)
        return { 
            data,
            message: '删除用户成功',
            code: 200
        }
    } catch (error: any) {
        console.error('删除用户失败:', error)
        return { 
            data: null,
            message: error.message || '删除用户失败',
            code: 500
        }
    }
}